/*
 * Copyright (c) 2011, Andrew Cole
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.andycole.xmiparser;

import com.martiansoftware.jsap.JSAPException;

import net.andycole.xmiparser.model.IModel;
import net.andycole.xmiparser.model.ModelFactory;
import net.andycole.xmiparser.model.ModelBuilder;
import net.andycole.xmiwriter.ModelFileWriter;

/**
 * The Main class for the AS3 to XMI converter. Will parse all .as and .mxml
 * files in the given path and will write a UML model as an XMI file.
 *
 * @author Andrew Cole
 */
public class Main
{
   private String _parentDir;
   private String _outFile;
   private String _xmiWriterFormat;

   /**
     * @param args The command line arguments of the form:
     *             -p <baseDir> [-x <format>] [-o <outputFileName>]
     *
     */
   public static void main(String[] args) throws JSAPException
   {
      int returnCode = startXMIWriting(args);
      System.exit(returnCode);
   }

   private static int startXMIWriting(String[] args) throws JSAPException
   {
      CommandLineHandler handler = new CommandLineHandler();

      int returnCode = 1;
      if (handler.areCommandLineOptionsCorrect(args))
      {
         final String xmiWriterFormat = handler.getParameterValue( CommandLineOptions.XMIWRITERFORMAT );
         final String outputFile = handler.getParameterValue( CommandLineOptions.OUTPUT );
         final String projectDir = handler.getParameterValue( CommandLineOptions.PROJECTDIR );

         final Main engine = new Main( projectDir,
                                       outputFile == null ? "xmiOutput.xmi" : outputFile,
                                       xmiWriterFormat == null ? "ArgoUML" : xmiWriterFormat);
         engine.run();
         returnCode = 0;
      }
      return returnCode;
   }

   public Main(String parentDirectory, String outFile, String xmiWriterFormat)
   {
      _parentDir = parentDirectory;
      _outFile = outFile;
      _xmiWriterFormat = xmiWriterFormat;
   }

   /**
    * Starts the conversion. Creates an appropriate metamodel, adds parsed
    * Actionscript content to it and writes the model to xmi using an
    * appropriate writer.
    */
   public void run()
   {
      IModel model = ModelFactory.getModel(_xmiWriterFormat);
      model.setUseMultiplicities(true);
      ModelBuilder builder = new ModelBuilder(_parentDir, model);
      builder.build();

      ModelFileWriter writer = new ModelFileWriter(model);
      writer.write(_outFile, _xmiWriterFormat);
   }
}
